<script>
import BaseUrl from "@/http/request/BaseUrl";
import TokenAxios from "@/http/request/TokenAxios";

export default {
  data() {
    return {
      showImagedetail: false,
      // 当前每页显示多少条数据
      pageSize: 6,
      //分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      tableData: [],
      resourcesType: '',
      total: 0,
      url: {},
      keyword: '',
      content: '',
    }
  },
  methods: {
    handleDialogClose() {
      // 关闭弹窗时处理视频暂停的逻辑
      const videos = document.querySelectorAll('video'); // 获取所有视频元素
      videos.forEach((video) => {
        video.pause(); // 暂停所有视频
      });
    },

    //删除笔记
    openDeleteConfirm(video) {
      let message = '此操作将删除文章，是否继续？';
      this.videoId = video.id
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete();
      }).catch(() => {
      });
    },
    handleDelete() {
      let url = BaseUrl.ADMIN_CONTENT + '/admin/video/delete';
      let deleteNote = {
        videoId: this.videoId
      }
      TokenAxios.post(url, deleteNote).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功!',
            type: 'success',
          });
          location.reload();
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch((error) => {
        console.log('请求出错:', error);
      });
    },
    //  查看图片详情
    selectImagedetail(video) {
      this.showImagedetail = true;
      console.log(video.id);
      this.content = video.content;
      console.log(video.content);
      let urls = BaseUrl.ADMIN_CONTENT + "/admin/video/" + video.id + "/picture";
      TokenAxios.get(urls).then((response) => {
        let JsonResult = response.data;
        if (JsonResult.state == 20000) {
          this.url = JsonResult.data; // 将返回的数据赋值给picUrl数组
          console.log(JsonResult.data)
        } else {
          // 处理状态不为 20000 的情况
        }
      }).catch((error) => {
        // 处理请求错误
        console.error(error);
      });
    },
    search() {
      console.log(this.keyword);
      let page = this.currentPage;
      let url = BaseUrl.ADMIN_CONTENT + '/admin/video/list/search?keyword=' + this.keyword + '&page=' + page;
      console.log(url)
      TokenAxios.get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult.data)
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.tableData.forEach((item) => {
            if (item.isDelete == 0) {
              item.isDelete = {
                text: '未删除',
                type: 'success'
              };
            } else {
              item.isDelete = {
                text: '已删除',
                type: 'danger'
              };
            }
          })
          console.log(this.tableData)
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
          this.maxPage = jsonResult.data.maxPage;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          })
        }
      })
    },
    // 切换分页
    handleCurrentChange(page) {
      if (this.keyword) {
        this.currentPage = page; // 更新当前页码值
        this.search(); // 执行搜索方法
      } else {
        this.$router.replace('?page=' + page);
        this.loadUserList();
      }
    },
    loadUserList() {
      "scope.row.resourcesType !== 1"
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = BaseUrl.ADMIN_CONTENT + '/admin/video/list?page=' + page;
      console.log('url=' + url);

      TokenAxios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.resourcesType = jsonResult.data;
          console.log('111111111111111111111111:' + this.resourcesType)
          this.tableData.forEach((item) => {
            if (item.isDelete == 0) {
              item.isDelete = {
                text: '未删除',
                type: 'success'
              };
            } else {
              item.isDelete = {
                text: '已删除',
                type: 'danger'
              };
            }
          })

          console.log(this.tableData)
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          })
        }
      })
    }
  },
  mounted() {
    this.loadUserList();
    var data = localStorage.getItem("loginInfo")
    var parse = JSON.parse(data);
    this.headers.Authorization = parse.token
    this.loadContents();
  }
}
</script>

<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;overflow: hidden">
      <el-breadcrumb-item :to="{ path: '/admin/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>視頻管理</el-breadcrumb-item>
      <el-breadcrumb-item>視頻列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 文章分类 -->
    <!--      <el-select-->
    <!--          clearable-->
    <!--          size="small"-->
    <!--          filterable-->
    <!--          placeholder="请选择分类"-->
    <!--          style="margin-right:1rem"-->
    <!--      >-->
    <!--        <el-option-->
    <!--            v-for="item in categoryList"-->
    <!--            :key="item.id"-->
    <!--            :label="item.categoryName"-->
    <!--            :value="item.categoryId"-->
    <!--        />-->
    <!--      </el-select>-->
    <!-- 文章名 -->
    <el-input
        clearable
        prefix-icon="el-icon-search"
        size="small"
        placeholder="请输入文章名"
        style="width:200px"
        v-model="keyword"
    ></el-input>
    <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        style="margin-left:1rem"
        @click="search()"
    >
      搜索
    </el-button>

    <el-card class="main-card">

      <el-table :data="tableData"
                style="width: 98%; margin-top:20px;">
        <el-table-column align="center" label="ID" prop="id">
        </el-table-column>
        <el-table-column align="center" label="昵称" prop="authorName"
                         :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="視頻封面" width="180" align="center" prop="coverUrl">
          <template slot-scope="scope">
            <div style="width: 115px;height: 115px;overflow: hidden;position: relative;top: 18px">
              <img :src="scope.row.coverUrl" width="100%" height="100%"
                   style="object-fit: cover;">
            </div>
            <br>
            <el-button type="text" @click="selectImagedetail(scope.row)" style="margin-right: 55px">查看详细</el-button>
            <el-dialog
                title="视频封面"
                :visible.sync="showImagedetail"
                width="50%"
                center
                @close="handleDialogClose"
            >
              <el-row>
                <el-col span:12>
                  <el-carousel trigger="click" height="600px">
                    <el-carousel-item>
                      <el-image fit="scale-down" :src="url.coverUrl" style="width: 100%; height: 100%"/>
                    </el-carousel-item>
                  </el-carousel>
                </el-col>
                <el-col span:12>
                  <p>{{ content }}</p>
                </el-col>
              </el-row>
            </el-dialog>
          </template>

        </el-table-column>


        <!-- 文章标题 -->
        <el-table-column prop="title" label="标题" align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <!-- 文章分类 -->
        <el-table-column prop="categoryName" label="分类" width="110px" align="center"
                         :show-overflow-tooltip="true"></el-table-column>


<!--        &lt;!&ndash; 文章ip &ndash;&gt;-->
<!--        <el-table-column-->
<!--            prop="ip"-->
<!--            label="ip"-->
<!--            align="center"-->
<!--        ></el-table-column>-->



        <el-table-column
            prop="isDelete"
            label="是否删除"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.isDelete.type">{{ scope.row.isDelete.text }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="gmtCreate" label="创建时间" align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="180px" align="center">
          <!-- eslint-disable-next-line -->
          <template slot-scope="scope">
            <!-- 删除文章按钮 -->
            <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="openDeleteConfirm(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :hide-on-single-page="true"
          :page-size="pageSize"
          layout="total, prev, pager, next,jumper"
          :total="total"
          background
      />
    </el-card>
  </div>
</template>

<style scoped>


.clearfix span {
  font-weight: 600;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

</style>